package com.keiou.drink.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.keiou.drink.R;
import com.keiou.drink.adapter.OrderHistoryAdapter;
import com.keiou.drink.api.AbsSubscriber;
import com.keiou.drink.api.exception.ApiException;
import com.keiou.drink.api.remote.Api;
import com.keiou.drink.api.rx.RxHelper;
import com.keiou.drink.response.OrderHistoryRes;
import com.keiou.drink.response.ResetStockRes;
import com.keiou.drink.response.base.HttpResult;
import com.keiou.drink.ui.activity.base.RxActivity;
import com.keiou.drink.util.CToast;
import com.keiou.drink.util.PrefsHelper;
import com.keiou.drink.widget.RestorePopup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者 ：Bob on 2017/10/30.
 * 邮箱 ：motherlovefather@126.com
 */

public class OrderHistoryActivity extends RxActivity {
    @BindView(R.id.iv_order_history)
    ImageView ivOrderHistory;
    @BindView(R.id.tv_restore)
    ImageView tvRestore;
    @BindView(R.id.rv_history)
    RecyclerView rvHistory;
    @BindView(R.id.root)
    RelativeLayout root;
    @BindView(R.id.v_bottom)
    View vBottom;

    private RestorePopup restorePopup;


    private OrderHistoryAdapter orderHistoryAdapter;
    private List<OrderHistoryRes.OrderBean> data = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_history;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        initRecyclerView();
        initAdapter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadData() {
        loadHistory();
    }


    private void loadHistory() {
        showLoadingDialog();
        addSubscribe(Api.getInstance().getOrderList(PrefsHelper.getDeviceId(this))
                .compose(RxHelper.applySchedulers())
                .subscribe(new AbsSubscriber<HttpResult<OrderHistoryRes>>() {
                    @Override
                    public void onNext(HttpResult<OrderHistoryRes> result) {
                        hideWaitDialog();

                        List<OrderHistoryRes.OrderBean> temp = result.getResult().getOrder();
                        if (null != temp) {
                            data.clear();
                            data.addAll(temp);
                        }

                        orderHistoryAdapter.notifyDataSetChanged();

                        if(data.size() >= 5){
                            vBottom.setVisibility(View.VISIBLE);
                        }else {
                            vBottom.setVisibility(View.GONE);
                        }

                    }


                    @Override
                    protected void onError(ApiException ex) {
                        hideWaitDialog();
                        CToast.showShort(OrderHistoryActivity.this, ex.getMsg());
                    }

                    @Override
                    protected void onIntercept(int code) {
                        onInterceptCode(code);
                    }
                }));
    }


    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvHistory.setLayoutManager(layoutManager);
    }

    private void initAdapter() {
        orderHistoryAdapter = new OrderHistoryAdapter(this, data);
        rvHistory.setAdapter(orderHistoryAdapter);
    }

    private void popupRestore() {
        if (null == restorePopup) {
            restorePopup = new RestorePopup(this);
            restorePopup.setOnSelectClickImpl(new RestorePopup.OnSelectClickImpl() {
                @Override
                public void onNoClick() {
                    restorePopup.dismiss();
                }

                @Override
                public void onYesClick() {
                    restorePopup.dismiss();
                    restore();
                }
            });
        }
        restorePopup.showAtLocation(findViewById(R.id.root), Gravity.CENTER, 0, 0);
    }


    @OnClick({R.id.iv_order_history, R.id.tv_restore})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_order_history:
                break;
            case R.id.tv_restore:
                popupRestore();
                break;
        }
    }

    private void restore() {
        showLoadingDialog();
        addSubscribe(Api.getInstance().resetStock(PrefsHelper.getDeviceId(this))
                .compose(RxHelper.applySchedulers())
                .subscribe(new AbsSubscriber<HttpResult<ResetStockRes>>() {
                    @Override
                    public void onNext(HttpResult<ResetStockRes> result) {
                        hideWaitDialog();
                        CToast.showShort(OrderHistoryActivity.this, "库存已恢复");
                    }


                    @Override
                    protected void onError(ApiException ex) {
                        hideWaitDialog();
                        CToast.showShort(OrderHistoryActivity.this, ex.getMsg());
                    }

                    @Override
                    protected void onIntercept(int code) {
                        onInterceptCode(code);
                    }
                }));

    }
}
